//
//  UIView+Extension.swift
//  RE_BasePrivateProject
//
//  Created by mac on 2018/7/18.
//  Copyright © 2018年 Regina. All rights reserved.
//

import Foundation
import UIKit
//import Toast_Swift

extension UIView {
    public static var classString: String {
        return String(describing: self)
    }

    public var viewController: UIViewController? {
        var n = self.next
        while n != nil {
            
            if (n is UIViewController) {
                
                return n as? UIViewController
            }
            
            n = n?.next
        }
        return nil
    }

    public func addBottomLine(_ height: CGFloat = 1, color: UIColor = REHexRGB(0xdbdbdb)) {

        let lineView: UIView = UIView()
        lineView.backgroundColor = color
        self.addSubview(lineView)

        lineView.snp.makeConstraints { (make) in
            make.height.equalTo(height)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    public func addTopLine(_ height: CGFloat = 1, color: UIColor = REHexRGB(0xdbdbdb)) {
        
        let lineView: UIView = UIView()
        lineView.backgroundColor = color
        self.addSubview(lineView)
        
        lineView.snp.makeConstraints { (make) in
            make.height.equalTo(height)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview()
        }
    }
    
    public func addRightLine(_ width: CGFloat = 1, color: UIColor = REHexRGB(0xdbdbdb), space: CGFloat = 0) {
        
        let lineView: UIView = UIView()
        lineView.backgroundColor = color
        self.addSubview(lineView)
        
        lineView.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.bottom.equalToSuperview().offset(-space)
            make.right.equalToSuperview()
            make.top.equalToSuperview().offset(space)
        }
    }
    
    func addCorner(_ roundingCorners: UIRectCorner, cornerSize: CGSize) {
        
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: roundingCorners, cornerRadii: cornerSize)
        let cornerLayer = CAShapeLayer()
        cornerLayer.frame = bounds
        cornerLayer.path = path.cgPath
        layer.mask = cornerLayer
    }
    
    //添加圆角
    func addCorner(_ roundingCorners: UIRectCorner, cornerSize: CGSize, frame: CGRect) {
        
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: roundingCorners, cornerRadii: cornerSize)
        let cornerLayer = CAShapeLayer()
        cornerLayer.frame = bounds
        cornerLayer.path = path.cgPath
        layer.mask = cornerLayer
    }
    
    //MARK:- 绘制虚线
    func drawDashLine(strokeColor: UIColor, width: CGFloat, lineWidth: CGFloat = 1, lineLength: Int = 5, lineSpacing: Int = 5, isBottom: Bool = true) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.bounds = self.bounds
        shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
        shapeLayer.fillColor = UIColor.white.cgColor
        shapeLayer.strokeColor = strokeColor.cgColor
        
        shapeLayer.lineWidth = lineWidth
        shapeLayer.lineJoin = kCALineJoinRound
        
        //每一段虚线长度 和 每两段虚线之间的间隔
        shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
        
        let path = CGMutablePath()
        let y = isBottom == true ? self.layer.bounds.height - lineWidth : 0
        path.move(to: CGPoint(x: 0, y: y))
        path.addLine(to: CGPoint(x: width, y: y))
        shapeLayer.path = path
        self.layer.addSublayer(shapeLayer)
    }

}


////MARK: - TOAST
//extension UIView {
//    public func re_showTast(_ message: String?) {
//        guard let str =  message?.trimmingWhiteSpaceCharacters() else {
//            return
//        }
//        if str.isBlank() {
//            return
//        }
//        
//        var toastStyle = ToastStyle()
//        toastStyle.backgroundColor = REHexRGBAlpha(0x000000, 0.7)
//        toastStyle.titleFont = REFontRegular(14)
//        toastStyle.titleColor = UIColor.white
//        toastStyle.verticalPadding = 10
//        toastStyle.horizontalPadding = 30
////        toastStyle.cornerRadius = 20
//        
//        self.makeToast(str, duration: 1, position: ToastPosition.center, title: nil, image: nil, style: toastStyle, completion: nil)
//        
//    }
//}
